﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System.Collections.Generic;

namespace UtilPublic.Redis
{
    /// <summary>
    /// SortedSet存储
    /// </summary>
    public partial class Rds
    {
        /// <summary>
        /// 加入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        public void StSetAdd<T>(string key, T value, double score) => RedisDb.SortedSetAdd(Zk(key), $"{value}", score);
        /// <summary>
        /// 增量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        public void StSetInc<T>(string key, T value, double score) => RedisDb.SortedSetIncrement(Zk(key), $"{value}", score);
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void StSetRemove<T>(string key, T value) => RedisDb.SortedSetRemove(Zk(key), $"{value}");
        /// <summary>
        /// 获取排行 默认降序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Descending">是否降序</param>
        /// <returns></returns>
        public long StSetRank<T>(string key, T value, bool Descending = true)
        {
            var order = Descending ? Order.Descending : Order.Ascending;
            var result = RedisDb.SortedSetRank(Zk(key), $"{value}", order);
            if (result == null)
            {
                return -1;
            }
            else
            {
                return (long)result + 1;
            }
        }
        /// <summary>
        /// 获取分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public double StSetScore<T>(string key, T value)
        {
            var result = RedisDb.SortedSetScore(Zk(key), $"{value}");
            return (result is null) ? 0 : (long)result;
        }
        /// <summary>
        /// 获取排行和分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="rank"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public void StSetRankScore<T>(string key, T value, out long rank, out double score)
        {
            rank = StSetRank(key, value);
            score = StSetScore(key, value);
        }
        /// <summary>
        /// 获取长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long StSetGetLength(string key) => RedisDb.SortedSetLength(Zk(key));

        /// <summary>
        /// 获取列表 根据排名
        /// </summary>
        /// <param name="key"></param>
        /// <param name="startRank"></param>
        /// <param name="stopRank"></param>
        /// <param name="Descending"></param>
        /// <returns></returns>
        public List<BaseTopDataRedis> StSetRangeByRank(string key, int startRank, int stopRank, bool Descending = false)
        {
            var result = new List<BaseTopDataRedis>();
            try
            {
                Order order = Order.Ascending;
                if (Descending)
                {
                    order = Order.Descending;
                }
                var rr = RedisDb.SortedSetRangeByRankWithScores(key, startRank, stopRank, order);
                int rank = 1;
                foreach (var nn in rr)
                {
                    result.Add(new BaseTopDataRedis(rank, nn.Element, (long)nn.Score));
                    rank++;
                }
            }
            catch
            {
                Zloger.Fatal($"StSetRangeByRank Error {key} {startRank} {stopRank}");
            }
            return result;
        }
    }
}